package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.CombinationDetailInfoDao;
import com.JZ.QM.common.dao.UserCollectDao;
import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.common.entity.CombinationDetailInfoEntity;
import com.JZ.QM.common.entity.UserCollectEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.dto.CollectAndCommentDto;
import com.JZ.QM.feign.CombinationDynamicInfoFeignService;
import com.JZ.QM.service.UserCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/6 9:35
 */
@Service()
public class UserCollectServiceImpl extends ServiceImpl<UserCollectDao, UserCollectEntity> implements UserCollectService {

    @Resource
    UserCollectDao userCollectDao;
    @Resource
    CombinationDynamicInfoFeignService combinationDynamicInfoFeignService;
    @Resource
    CombinationDetailInfoDao combinationDetailInfoDao;

    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<UserCollectEntity> page = this.page(
                new Query<UserCollectEntity>().getPage(params),
                new QueryWrapper<UserCollectEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 用户收藏组合产品
     * @param collectAndCommentDto 收藏数据传输对象
     * @return
     */
    @Transactional
    @Override
    public boolean collect(CollectAndCommentDto collectAndCommentDto) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //用户收藏基本信息
        UserCollectEntity userCollectEntity=new UserCollectEntity();
        //用户更新状态
        boolean collectState=false;
        int affectRowUserCollect=0;

        //构建userCollect,插入收藏信息
        userCollectEntity.setComBaseId(collectAndCommentDto.getComBaseId());
        userCollectEntity.setUserId(collectAndCommentDto.getUserId());
        //构建查询信息
        queryWrapper.eq("user_id",collectAndCommentDto.getUserId());
        queryWrapper.eq("com_base_id",collectAndCommentDto.getComBaseId());

        //插入或者更新数据
        if(userCollectDao.selectCount(queryWrapper) == 0){
            affectRowUserCollect=userCollectDao.insert(userCollectEntity);
        } else {
            userCollectEntity.setIsUsed(collectAndCommentDto.getCollectState());
            affectRowUserCollect=userCollectDao.update(userCollectEntity,queryWrapper);
        }

        //更新组合动态信息
        if (collectAndCommentDto.getCollectState() == 1){
            collectState= (Boolean) combinationDynamicInfoFeignService.updateCombinationDynamicInfo(
                    collectAndCommentDto.getComBaseId(),
                    1,
                    0,
                    0,
                    0).get("comDynamicState");
        } else {
            collectState= (Boolean) combinationDynamicInfoFeignService.updateCombinationDynamicInfo(
                    collectAndCommentDto.getComBaseId(),
                    -1,
                    0,
                    0,
                    0).get("comDynamicState");
        }
        //判断是否每一个表都成功执行
        if(collectState && affectRowUserCollect !=0){
            return true;
        } else {
            return false;
        }


    }

    /**
     * 虎丘 用户收藏的组合列表
     * @param userId 用户id
     * @return
     */
    @Override
    public ArrayList<CombinationDetailInfoEntity> getUserCollectComList(Long userId) {
        //组合详细信息列表
        ArrayList<CombinationDetailInfoEntity> combinationDetailInfoEntities=new ArrayList<>();

        combinationDetailInfoEntities= (ArrayList<CombinationDetailInfoEntity>) combinationDetailInfoDao.getUserCollectComList(userId);
        return combinationDetailInfoEntities;
    }

    /**
     * 获取组合收藏的状态
     * @param userId 用户id
     * @param comBaseId 组合id
     * @return
     */
    @Override
    public Boolean getComCollectState(Long userId, Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合的收藏状态
        Boolean collectState=false;
        //符合条件的组合的数量
        Integer comCollectNum=0;

        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("com_base_id",comBaseId);
        queryWrapper.eq("is_used",1);

        //收藏组合的数量
        comCollectNum=userCollectDao.selectCount(queryWrapper);

        if(comCollectNum >0){
            return true;
        } else {
            return false;
        }

    }
}
